//
//  UserSetUsernameViewController.swift
//  SitPosition
//
//  Created by apple on 2018/12/31.
//  Copyright © 2018 apple. All rights reserved.
//

import Foundation
import UIKit
import Cartography


class UserSetUsernameViewController : UserLoginRegistrationBaseViewController {
    
    // MARK: - Properties
    
    // 完成用户名设置回调
    var onSettedupBlock: (/*username*/String) -> () = { _ in }
    
    // MARK: - Subviews
    
    // 用户名
    lazy var usernameTextfield: TextFieldWithLRView = {
        return _buildTextField(placeholder: Strings.logonUsernamePlaceholder,
                               leftView: UIImageView(image: UIImage(named: "logon_name")))
    }()
    
    // 完成按钮
    lazy var finishButton: UserLogonStateButton = {
        let button = UserLogonStateButton(title: Strings.logonFinishRegistion)
        button.addTarget(self, action: #selector(_step2Finish), for: UIControl.Event.touchUpInside)
        return button
    }()
    
    let seperator1 = UIView.colored(Theme.Color.primaryBlueSeperatorColor)
    
    // MARK: - Object lifecycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        gk_navLeftBarButtonItem = UIBarButtonItem(imageName: "ic_nav_back", target: self, action: #selector(_backButtonTapped))
        
        // 设置Views
        view.addSubviews([
            titleLabel,
            usernameTextfield,
            seperator1,
            finishButton
            ])
        _setupContraints()
        
        // 初始化Views
        titleLabel.text = Strings.logonRegister
    }
}

extension UserSetUsernameViewController {
    
    // 设置视图约束
    func _setupContraints() {
        // titleLabel
        constrain(titleLabel, gk_navigationBar, view) { v, sibling, parent in
            v.top == sibling.bottom + 34
            v.left == parent.left + 40
            v.right == parent.right - 40
            v.height == 25
        }
        
        // usernameTextfield
        let kTextfieldHeight: CGFloat = 36
        constrain(usernameTextfield, titleLabel) { v, sibling in
            v.top == sibling.bottom + 48
            v.left == sibling.left
            v.height == kTextfieldHeight
            v.right == sibling.right
        }
        
        // seperator1
        constrain(seperator1, usernameTextfield) { v, sibling in
            v.top == sibling.bottom+4
            v.left == sibling.left
            v.right == sibling.right
            v.height == 1
        }
        
        // finishButton
        constrain(finishButton, seperator1) { v, sibling in
            v.top == sibling.bottom + 26
            v.left == sibling.left
            v.width == 200
            v.height == 55
        }
    }
    
    // 返回按钮点击事件
    @objc func _backButtonTapped() {
        backCallback()
    }
    
    // 完成
    @objc func _step2Finish() {
        // 验证用户名有效性
        guard let username = usernameTextfield.text else {
            view.makeToast(Strings.logonUsernameEmpty)
            return
        }
        
        // 完成设置用户名
        onSettedupBlock(username)
    }
}
